﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{
    public Transform Target;
    public Transform mainCamera;
    public float smoothTime = 0.2f;
    private Vector3 velocity = Vector3.zero;

    //持续抖动的时长
    public float shake = 0f;

    // 抖动幅度（振幅）
    //振幅越大抖动越厉害
    public float shakeAmount = 0.5f;
    public float decreaseFactor = 1.0f;

    Vector3 originalPos;
    void Awake()
    {
        if (mainCamera == null)
        {
            mainCamera = GetComponent(typeof(Transform)) as Transform;
        }
    }


    void LateUpdate()
    {
        if (shake > 0)
        {

            mainCamera.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;

            shake -= Time.deltaTime * decreaseFactor;
        }
        else
        {
            shake = 0f;
 
            if (null != Target)
            {
                mainCamera.transform.position = Vector3.SmoothDamp(mainCamera.transform.position, Target.TransformPoint(new Vector3(0, 0, -10)), ref velocity, smoothTime);
            }
        }

      
    }
    public void Shake(float shake = 0.5f)
    {
        this.shake = shake;
        originalPos = mainCamera.localPosition;
    }
}
